package com.example.housingRentalSystem.exception;

import com.example.housingRentalSystem.utils.ApiResponse;
import com.example.housingRentalSystem.utils.HttpStatus;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.validation.BindException;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import javax.validation.ConstraintViolationException;
import javax.validation.GroupDefinitionException;

@RestControllerAdvice
public class GlobalExceptionHandler {

    private static final Logger logger = LoggerFactory.getLogger(GlobalExceptionHandler.class);

    @ExceptionHandler(GroupDefinitionException.class)
    public ApiResponse<Void> handleGroupDefinition(GroupDefinitionException e) {
        logger.error("验证组定义异常: {}", e.getMessage());
        return ApiResponse.error(
                HttpStatus.BAD_REQUEST.getCode(),
                "数据校验失败: " + e.getMessage()
        );
    }


    @ExceptionHandler(RuntimeException.class)
    public ApiResponse<String> handleRuntimeException(RuntimeException e) {
        logger.error("运行时异常: {}", e.getMessage(), e);
        return ApiResponse.error(HttpStatus.BAD_REQUEST.getCode(), e.getMessage());
    }

    /**
     * 处理空指针异常
     */
    @ExceptionHandler(NullPointerException.class)
    public ApiResponse<String> handleNullPointerException(NullPointerException e) {
        logger.error("空指针异常: {}", e.getMessage(), e);
        return ApiResponse.error(HttpStatus.BAD_REQUEST.getCode(), "空指针异常");
    }

    /**
     * 处理非法参数异常
     */
    @ExceptionHandler(IllegalArgumentException.class)
    public ApiResponse<String> handleIllegalArgumentException(IllegalArgumentException e) {
        logger.error("非法参数异常: {}", e.getMessage(), e);
        return ApiResponse.error(HttpStatus.BAD_REQUEST.getCode(), "非法参数异常");
    }

    /**
     * 处理参数校验异常
     */
    @ExceptionHandler(MethodArgumentNotValidException.class)
    public ApiResponse<String> handleMethodArgumentNotValidException(MethodArgumentNotValidException e) {
        logger.error("参数校验异常: {}", e.getMessage(), e);
        return ApiResponse.error(HttpStatus.BAD_REQUEST.getCode(), e.getBindingResult().getFieldError().getDefaultMessage());
    }

    /**
     * 处理参数绑定异常
     */
    @ExceptionHandler(BindException.class)
    public ApiResponse<String> handleBindException(BindException e) {
        logger.error("参数绑定异常: {}", e.getMessage(), e);
        return ApiResponse.error(HttpStatus.BAD_REQUEST.getCode(), e.getBindingResult().getFieldError().getDefaultMessage());
    }

    /**
     * 处理请求体解析异常
     */
    @ExceptionHandler(HttpMessageNotReadableException.class)
    public ApiResponse<String> handleHttpMessageNotReadableException(HttpMessageNotReadableException e) {
        logger.error("请求体解析异常: {}", e.getMessage(), e);
        return ApiResponse.error(HttpStatus.BAD_REQUEST.getCode(), "请求体解析失败");
    }

    /**
     * 处理约束校验异常
     */
    @ExceptionHandler(ConstraintViolationException.class)
    public ApiResponse<String> handleConstraintViolationException(ConstraintViolationException e) {
        logger.error("约束校验异常: {}", e.getMessage(), e);
        return ApiResponse.error(HttpStatus.BAD_REQUEST.getCode(), e.getMessage());
    }

    /**
     * 处理其他异常
     */
    @ExceptionHandler(Exception.class)
    public ApiResponse<String> handleException(Exception e) {
        logger.error("系统异常: {}", e.getMessage(), e);
        return ApiResponse.error(HttpStatus.INTERNAL_SERVER_ERROR.getCode(), "系统异常");
    }

}